package com.asj.service.command;

import static com.asj.enums.AppIdentifierDictEnums.is_lasted_op;
import static com.asj.enums.AppIdentifierDictEnums.supervision_NotSave;
import static com.asj.enums.AppIdentifierDictEnums.supervision_cancel;
import static com.asj.enums.ControlProgressBarEnum.clearBlackList;
import static com.asj.enums.DeviceIdentifierDictEnums.DEVICE_BATTER_PERCENTAGE;
import static com.asj.enums.DeviceIdentifierDictEnums.DEVICE_INFO;
import static com.asj.enums.DeviceIdentifierDictEnums.DEVICE_PHONE_NUMBER;
import static com.asj.enums.DeviceIdentifierDictEnums.DEVICE_POSITION;
import static com.asj.enums.DeviceIdentifierDictEnums.appVersionUpdate;
import static com.asj.enums.DeviceIdentifierDictEnums.cancelSupervision;
import static com.asj.enums.DeviceIdentifierDictEnums.clearScreenLockPassword;
import static com.asj.enums.DeviceIdentifierDictEnums.deviceRecovery;
import static com.asj.enums.DeviceIdentifierDictEnums.functionRestrict;
import static com.asj.enums.DeviceIdentifierDictEnums.json_key_device_operation_info;
import static com.asj.enums.DeviceIdentifierDictEnums.lockOperation;
import static com.asj.enums.DeviceIdentifierDictEnums.restrictAppBlack;
import static com.asj.enums.DeviceIdentifierDictEnums.syncDeviceInfo;
import static com.asj.enums.DeviceIdentifierDictEnums.updateAppLogoAndName;


import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;

import com.asj.MainActivity;
import com.asj.service.command.specific.SpecificAppVersionUpdateService;
import com.asj.service.command.specific.SpecificCancelSupervisionService;
import com.asj.service.command.specific.SpecificClearScreenLockPasswordService;
import com.asj.service.command.specific.SpecificDeviceRecoveryService;
import com.asj.service.command.specific.SpecificFunctionRestrictService;
import com.asj.service.command.specific.SpecificLockOperationService;
import com.asj.service.command.specific.SpecificRestrictAppBlackService;
import com.asj.service.command.specific.SpecificSyncDeviceInfoService;
import com.asj.service.command.specific.SpecificUpdateAppLogoAndNameService;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;


/**
 * 指令接收服务
 * */
public class CommandReceptionUpdateService {
    @SuppressLint("StaticFieldLeak")
    private static Context context;
    @SuppressLint("StaticFieldLeak")
    private static CommandReceptionUpdateService instance;

    public CommandReceptionUpdateService(Context context){
        CommandReceptionUpdateService.context = context;
    }

    public static synchronized CommandReceptionUpdateService getInstance(Context context) {
        if (instance == null) {
            instance = new CommandReceptionUpdateService(context);
        }
        return instance;
    }

    /**
     * MQ消息处理中心
     * */
    public  void onProgressUpdate(String... messages) {
        JsonObject contentData = JsonParser.parseString(messages[0]).getAsJsonObject();
        String operationInfo = contentData.get(json_key_device_operation_info.getValue()).getAsString();
        if(operationInfo == null){
            //todo:异常发送给全局日志
            Log.e("MQ消息处理异常！","接收操作指令的值为null");
            return;
        }
        /* 功能限制操作 */
        if(operationInfo.equals(functionRestrict.getValue())){
            SpecificFunctionRestrictService.operatingCenter(contentData,context);
        }
        /* 监听锁机操作 */
        else if(operationInfo.equals(lockOperation.getValue())){
            SpecificLockOperationService.lockOperation(contentData,context);
        }
        /* 取消监管操作 */
        else if(operationInfo.equals(cancelSupervision.getValue())){
            SpecificCancelSupervisionService.cancelSupervision(contentData,context);
        }
        /* 监听设置黑名单列表 */
        else if(operationInfo.equals(restrictAppBlack.getValue())){
            SpecificRestrictAppBlackService.restrictAppBlack(contentData,context);
        }
        /* 监听设置恢复出厂设置操作 */
        else if(operationInfo.equals(deviceRecovery.getValue())){
            SpecificDeviceRecoveryService.deviceRecovery(contentData,context);
        }
        /* 同步设备信息 */
        else if(operationInfo.equals(syncDeviceInfo.getValue())){
            SpecificSyncDeviceInfoService.syncDeviceInfo(contentData,context);
        }
        /* 清除锁屏密码 */
        else if(operationInfo.equals(clearScreenLockPassword.getValue())){
            SpecificClearScreenLockPasswordService.clearScreenLockPassword(contentData,context);
        }
        /* 版本更新推送 */
        else if (operationInfo.equals(appVersionUpdate.getValue())){
            SpecificAppVersionUpdateService.appVersionUpdate(contentData,context);
        }
        /* 更换appLogo和名称 */
        else if (operationInfo.equals(updateAppLogoAndName.getValue())){
            SpecificUpdateAppLogoAndNameService.updateAppLogoAndName(context);
        }
    }
}
